#+_+ coding:utf-8 +_+

import tornado.web
import auth
import storage 
from base import BaseHdl
from common import errno
from common import buffer
from storage import news

class NewsHdl(BaseHdl):
    def get(self):
        uid = self.get_argument("uid","0")
        signature = self.get_argument("signature","")
        newsID = self.get_argument("news_id","0")

        rst = auth.IsValiedUID(uid)
        if not rst:
            rep = self.uidError()
        else:
            ticket = storage.uid.UID.GetTicket(uid)
            rst = auth.CheckSignature(signature,uid,newsID,ticket)
            if not rst :
                rep = self.signatureError()
            else:
                rst = news.CheckNewsID(newsID)
                if not rst :
                    rep  = self.newsIDError()
                else:
                    rep = self.news(newsID)
        self.write(rep)

    def newsIDError(self):
        msg = dict(errno=errno.ENEWS_ID_NOTVALIED,errmsg=errno.ErrStr(errno.ENEWS_ID_NOTVALIED))
        rep = dict(rst="FAILED",msg=msg)
        return buffer.JsonEncode(rep)

    def news(self,newsID):
        newsCont = news.GetNews(newsID)
        msg = dict(id     = newsCont["id"],
                   author = newsCont["author"],
                   title  = newsCont["title"],
                   content= newsCont["content"],
                   star   = newsCont["star"])
        
        rep = dict(rst="SUCCESS",msg=msg)
        return buffer.JsonEncode(rep)
        



